package com.borun.dictionary;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.borun.bean.Word;
import com.borun.dao.WordDao;

public class UpdateWord extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	private int word_id = 0;
	private WordDao dao = new WordDao();
	private JLabel lblEnglish, lblChinese;
	private JTextField txtEnglish, txtChinese;
	private JButton btnUpdate, btnCancel;

	public UpdateWord(String wordid) {

		super("修改单词");
		this.setLayout(null);

		word_id = Integer.valueOf(wordid);
		
		lblEnglish = new JLabel("英语单词：");
		lblEnglish.setBounds(30, 20, 100, 30);

		txtEnglish = new JTextField();
		txtEnglish.setBounds(100, 20, 160, 30);

		lblChinese = new JLabel("汉语意思：");
		lblChinese.setBounds(30, 60, 100, 30);

		txtChinese = new JTextField();
		txtChinese.setBounds(100, 60, 160, 30);

		btnUpdate = new JButton("修改");
		btnUpdate.addActionListener(this);
		btnUpdate.setBounds(50, 120, 80, 30);

		btnCancel = new JButton("取消");
		btnCancel.addActionListener(this);
		btnCancel.setBounds(160, 120, 80, 30);

		try {
			Word word = dao.getWord(Integer.valueOf(wordid));
			txtEnglish.setText(word.getEnglish());
			txtChinese.setText(word.getChinese());
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		this.add(lblEnglish);
		this.add(txtEnglish);
		this.add(lblChinese);
		this.add(txtChinese);
		this.add(btnUpdate);
		this.add(btnCancel);

		this.setSize(300, 200);
		this.setLocationRelativeTo(null); // 设置窗口相对于指定组件的位置
		this.setResizable(false); // 禁止改变窗体大小
		// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

//	public static void main(String[] args) {
//		new UpdateWord("1");
//	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnUpdate) {
			String english = txtEnglish.getText();
			if ("".equals(english) || english.trim().isEmpty()) {
				JOptionPane.showMessageDialog(null, "请正确填写英语单词！");
				return;
			}
			String chinese = txtChinese.getText();
			if ("".equals(chinese) || chinese.trim().isEmpty()) {
				JOptionPane.showMessageDialog(null, "请正确填写汉语意思！");
				return;
			}

			Word word = new Word(word_id,english, chinese);
			try {
				if (dao.updateWord(word)) {
					JOptionPane.showMessageDialog(null, "更新成功！");
					this.setVisible(false);
				} else {
					JOptionPane.showMessageDialog(null, "系统异常，更新失败！");
				}
			} catch (HeadlessException e1) {
				e1.printStackTrace();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == btnCancel) {
			this.setVisible(false);
		}
	}

}
